package com.qaml.mes.device.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qaml.mes.device.dao.EqpRepairDO;
import com.qaml.mes.device.dao.mapper.EqpRepairMapper;
import com.qaml.mes.device.repository.EqpRepairRepository;
import com.qaml.mes.device.dto.eqp.EqpRepairDTO;
import com.qaml.mes.device.enums.EqpRepairStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Objects;

/**
 * @ClassName EqpRepairRepositoryImpl
 * @Description TODO
 * @Author leikai
 * @Date 2024/3/4 0004 11:26
 * @Version 1.0
 */
@Repository
public class EqpRepairRepositoryImpl implements EqpRepairRepository {

    @Resource
    private EqpRepairMapper eqpRepairMapper;

    @Override
    public Long countByUserId(String userId) {
        LambdaQueryWrapper<EqpRepairDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EqpRepairDO::getUserId, userId);
        queryWrapper.eq(EqpRepairDO::getIsDeleted, 0);
        queryWrapper.eq(EqpRepairDO::getStatus, EqpRepairStatusEnum.COMPLETING.getType());
        return eqpRepairMapper.selectCount(queryWrapper);
    }

    @Override
    public void saveBatch(ArrayList<EqpRepairDO> eqpRepairDOS) {
        eqpRepairMapper.saveBatch(eqpRepairDOS);

    }

    public int updateById(EqpRepairDO eqpRepairDO) {
        return eqpRepairMapper.updateById(eqpRepairDO);
    }

    /**
     * app 设备维修列表查询
     *
     * @param page
     * @param pageSize
     * @param eqpRepairDTO
     * @return
     */
    @Override
    public IPage<EqpRepairDO> repairList(Integer page, Integer pageSize, EqpRepairDTO eqpRepairDTO) {
        LambdaQueryWrapper<EqpRepairDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EqpRepairDO::getIsDeleted, 0);
        queryWrapper.eq(StringUtils.isNotBlank(eqpRepairDTO.getEqpCode()), EqpRepairDO::getEqpCode, eqpRepairDTO.getEqpCode());
        queryWrapper.eq(Objects.nonNull(eqpRepairDTO.getStatus()), EqpRepairDO::getStatus,
                eqpRepairDTO.getStatus());
        queryWrapper.orderByDesc(EqpRepairDO::getId);
        return eqpRepairMapper.selectPage(new Page<>(page, pageSize), queryWrapper);
    }

    @Override
    public void deleteByRecordId(String recordId) {
        eqpRepairMapper.deleteByRecordId(recordId);
    }

    @Override
    public void insert(EqpRepairDO eqpRepairDO) {
        eqpRepairMapper.insert(eqpRepairDO);
    }
}